package com.yange.accountbook.ui

import android.content.Context
import android.content.Intent
import android.text.TextUtils
import android.widget.ImageView
import android.widget.RelativeLayout
import androidx.recyclerview.widget.GridLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.yange.accountbook.R
import com.yange.accountbook.databinding.AddDetailedTypeLayoutBinding
import com.yange.accountbook.net.Api
import com.yange.accountbook.net.BaseResponse
import com.yange.accountbook.net.api.RetrofitClient
import com.yange.accountbook.net.observable.RecObserver
import com.yange.accountbook.utils.image.loadCircleImg
import com.yange.accountbook.utils.image.loadHeadImg
import com.yange.accountbook.utils.showToast

class AddDetailedTypeActivity:BaseActivity<AddDetailedTypeLayoutBinding>() {

    var iconUrl:String = ""
    lateinit var iconAdapter:IconAdapter

    override fun getViewBinding() = AddDetailedTypeLayoutBinding.inflate(layoutInflater)

    override fun init() {
        iconAdapter = IconAdapter()
        binding.listData.layoutManager = GridLayoutManager(this,5)
        binding.listData.adapter = iconAdapter
        iconAdapter.setOnItemClickListener { adapter, view, position ->
            var item:String = adapter.getItem(position) as String
            iconUrl = item
            iconAdapter.setChecked(position)
            loadHeadImg(binding.ivIcon,iconUrl)
        }
        getIconList()
        binding.titleBar.tvForward?.setOnClickListener {
            if(TextUtils.isEmpty(iconUrl)){
                showToast("请选择图标")
            }else if (TextUtils.isEmpty(binding.editTitle.text)){
                showToast("请输入名称")
            }else{
                saveItem()
            }

        }
    }


    private fun saveItem(){
         RetrofitClient.getInstance().invokePostBody(this,Api.FEEITEM_SAVE, mapOf(
             "icon" to iconUrl,
             "moneyType" to intent.getStringExtra("type"),
             "name" to binding.editTitle.text.toString().trim(),
         )).subscribe(object :RecObserver<BaseResponse<Any>>(this,true,false){
             override fun onSuccess(t: BaseResponse<Any>?) {
                    showToast("添加成功")
                    finish()
             }

             override fun onFail(msg: String, code: Int) {
                showToast(msg)
             }
         })
    }



    private fun getIconList(){
        RetrofitClient.getInstance().invokeGet(this,Api.GET_ICON)
            .subscribe(object : RecObserver<BaseResponse<List<String>>>(this,true,false){
                override fun onSuccess(t: BaseResponse<List<String>>?) {
                    if(!t?.data.isNullOrEmpty()){
                        iconAdapter.setNewInstance(t?.data as MutableList<String>?)
                    }
                }
                override fun onFail(msg: String, code: Int) {
                    showToast(msg)
                }
            })
    }


    class IconAdapter : BaseQuickAdapter<String, BaseViewHolder>(R.layout.books_icon_item_layout) {
        var checkedPosition = -1
        override fun convert(holder: BaseViewHolder, item: String) {
            var image = holder.getView<ImageView>(R.id.iv_icon)
            var layout = holder.getView<RelativeLayout>(R.id.layout_icon)
            holder.setGone(R.id.tv_title,true)
            context.loadCircleImg(image,item)
            if (holder.layoutPosition == checkedPosition) {
                //设置为选中
                layout.setBackgroundResource(R.drawable.round_checked)
            } else {
                layout.setBackgroundResource(R.drawable.round_normal)
            }
        }

        fun setChecked(int: Int) {
            checkedPosition = int
            notifyDataSetChanged()
        }
    }


    companion object{
        /**
         * type 类型：IN,OUT
         */
        fun start(context: Context,type:String){
            var intent = Intent(context,AddDetailedTypeActivity().javaClass)
            intent.putExtra("type",type)
            context.startActivity(intent)
        }
    }
}